package cn.impl;

import cn.annotation.Log;
import cn.basic.HibBaseDAOImpl;
import cn.basic.iHibBaseDAO;
import cn.common.properties.LogOperType;
import cn.dao.RoleDAO;
import cn.model.TRole;
import org.springframework.stereotype.Component;

import java.util.List;


@Component("roledao")
public class RoleDAOImpl implements RoleDAO {

    private iHibBaseDAO bdao = null;

    public RoleDAOImpl(){
        bdao = new HibBaseDAOImpl();
    }

    @Log(isSaveLog=true,operationType= LogOperType.ADD,operationName="添加角色")
    @Override
    public int addRole(TRole Role) {
        return (Integer) bdao.insert(Role);
    }

    @Log(isSaveLog=true,operationType= LogOperType.DELETE,operationName="删除角色")
    @Override
    public boolean deleteRole(int Roleid) {
        TRole TRole = (cn.model.TRole) bdao.findById(cn.model.TRole.class, Roleid);
        return bdao.delete(TRole);
    }

    @Log(isSaveLog=true,operationType= LogOperType.MODIFY,operationName="修改角色")
    @Override
    public boolean modifyRole(TRole Role) {
        return bdao.update(Role);
    }

    @Log(isSaveLog=false)
    @Override
    public TRole getRoleById(String rolename) {
        return (TRole) bdao.findById(TRole.class, rolename);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询角色")
    @Override
    public List<TRole> getAllRoleListByPage(String wherecondition,int currentPage, int pageSize) {
        String hql = "from TRole";
        if (wherecondition == null && wherecondition.equals("")){
            hql += "order by roleid desc";
        }
        if (wherecondition != null && !wherecondition.equals("")) {
            hql += wherecondition;
        }
        List<TRole> list = bdao.selectByPage(hql, currentPage, pageSize);
        return list;
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询角色")
    @Override
    public int getAllRolePageAmount(String wherecondition) {
        String hql = "select count(*) from TRole";
        if (wherecondition == null && wherecondition.equals("")) {
            hql += wherecondition;
        }
        return bdao.selectValue(hql);
    }

    @Log(isSaveLog=false)
    @Override
    public List<TRole> getAllRoles() {
        String hql = "from TRole";
        return (List<TRole>)bdao.select(hql);
    }

    /**
     * @param wherecondition
     * @param currentPage    按分页查询的当前页
     * @param pageSize       按分页查询的每页数量
     * @return List
     */
    @Log(isSaveLog=false)
    @Override
    public List<TRole> getRoleListByPage(String wherecondition, int currentPage, int pageSize) {
        String hql = "from TRole order by id desc";
        if (wherecondition !=null &&!wherecondition.equals("")){
            hql += wherecondition;
        }
        List<TRole> list = bdao.selectByPage(hql, currentPage, pageSize);
        return list;
    }

    @Log(isSaveLog=false)
    @Override
    public int getRoleAmount() {
        String hql = "select count(*) from TRole";
        return bdao.selectValue(hql);
    }
}
